# -*- coding: utf-8 -*-
"""
@Version : 
@Author  : ChenMZ
@Time    : 2025/09/16
@File    : global.py
@Desc    : 
@Modify  : 
"""
import enum
from typing import Union, Dict
from PyQt5.QtCore import Qt
from PyQt5.QtSvg import QSvgRenderer
from PyQt5.QtGui import QPixmap, QPainter, QColor, QIcon
from qfluentwidgets import getIconColor, Theme, FluentIconBase
from device import DevGroups


class ColorTheme(enum.Enum):
    light_theme = QColor("#1296db")
    dark_theme = QColor("#bfbfbf")


class DevIcon(enum.Enum):
    CPU = "CPU", ":/navigation/icons/cpu.svg"
    GPUs = "GPU", ":/navigation/icons/gpu.svg"
    ETHs = "以太网", ":/navigation/icons/eth.svg"

    def __init__(self, localization: str, icon: str):
        self.localization_str = localization
        self.icon_path = icon

    @property
    def tr_zh(self):
        return self.localization_str

    @property
    def icon(self):
        return self.icon_path

    @classmethod
    def members(cls) -> Dict[str, 'DevIcon']:
        return cls.__members__


def svg_to_pixmap(svg_filename: str, width: int, height: int, color: Union[QColor, ColorTheme]) -> QPixmap:
    """
    将SVG文件渲染为指定颜色的QPixmap

    参数:
        svg_filename: SVG文件路径
        width: 生成图片的宽度
        height: 生成图片的高度
        color: 想要设置的颜色 (QColor对象)

    返回:
        QPixmap对象
    """
    # 创建SVG渲染器并加载SVG文件
    renderer = QSvgRenderer(svg_filename)
    # 创建指定大小的透明QPixmap
    pixmap = QPixmap(width, height)
    pixmap.fill(Qt.transparent)  # 填充透明背景

    # 在pixmap上绘制
    painter = QPainter(pixmap)
    # 先将SVG渲染到pixmap上（此时是其原始颜色或黑色，但alpha通道很重要）
    renderer.render(painter)
    # 设置组合模式：SourceIn意味着新颜色只应用于目标（即刚渲染的SVG）的可见区域
    painter.setCompositionMode(QPainter.CompositionMode_SourceIn)
    # 用指定颜色填充整个区域，由于组合模式，只有SVG形状部分会应用该颜色
    if isinstance(color, QColor):
        painter.fillRect(pixmap.rect(), color)
    else:
        x, y, w, h = pixmap.rect().x(), pixmap.rect().y(), pixmap.rect().width(), pixmap.rect().height()
        painter.fillRect(x, y, w, h, color.value)
    # 结束绘制
    painter.end()

    return pixmap
